package com.czk.rdd.operator.transform

import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author:ChenZhangKun
 * @Date: 2021/12/9 17:22
 */
object FoldByKey {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("RDD")
    val sc = new SparkContext(sparkConf)

    val rdd = sc.makeRDD(List(("a", 1), ("a", 2), ("a", 3), ("a", 4), ("b", 5)), 2)
    // 当分区内计算规则和分区间计算规则相同时，aggregateByKey 就可以简化为 foldByK
    rdd.foldByKey(0)(_ + _).collect().foreach(println)
  }
}
